<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>登入</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="js/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="css/admin.css" media="all">
  <link rel="stylesheet" href="css/login.css" media="all">
</head>
<body>

  <div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
      <div class="layadmin-user-login-box layadmin-user-login-header">
        <h2>用户登录</h2>
        <p></p>
      </div>
      <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
          <input type="text" name="email" id="LAY-user-login-username" lay-verify="required" placeholder="邮箱" class="layui-input">
        </div>
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
          <input type="password" name="password" id="LAY-user-login-password" lay-verify="required" placeholder="密码" class="layui-input">
        </div>
        
        <div class="layui-form-item">
          <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
        </div>
        <div class="layui-trans layui-form-item layadmin-user-login-other">
          <a href="reg.html" class="layadmin-user-jump-change layadmin-link">注册帐号</a>
        </div>
      </div>
    </div>
    
    <div class="layui-trans layadmin-user-login-footer">
      
    </div>
    
    
  </div>

  <script src="js/layui/layui.js"></script>  
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
    $.ajaxSetup({
		complete:function(XMLHttpRequest,textStatus){
			console.log(XMLHttpRequest,textStatus);
             if(XMLHttpRequest.status == 401){
                 //会话失效，跳转到登录页
                 window.location.replace("login.html");
             }else if(XMLHttpRequest.status == 303){
                 alert(XMLHttpRequest.responseText);
             }
          }
	});
	
  layui.use(['form'], function(){
    var form = layui.form;

    form.render();

    //提交
    form.on('submit(LAY-user-login-submit)', function(obj){
    
		console.log(obj.field);
      //请求登入接口
      $.ajax({
        url: "/api/user/login/"+obj.field.email+"/"+obj.field.password
        ,data: {}
        ,success: function(res){
			console.log(res);
			if(!res){
				layer.msg('邮箱或密码错误！', {
					offset: '15px'
					,icon: 2
					,time: 1000
				  });
				return;
			}
          sessionStorage.setItem( "email", res);
		  
          //登入成功的提示与跳转
          layer.msg('登入成功', {
            offset: '15px'
            ,icon: 1
            ,time: 1000
          }, function(){
            location.href = 'main.html'; //后台主页
          });
        }
      });
      
    });
    
    
  });
  </script>
</body>
</html>